package cn.hnu.data;


import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class UseRecord {
    // uid, stationId, begintime, endtime, money
    private int uid;
    private int stationId;
    private String begintime;
    private String endtime;
    private int money;

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public int getStationId() {
        return stationId;
    }

    public void setStationId(int stationId) {
        this.stationId = stationId;
    }

    public String getBegintime() {
        return begintime;
    }

    public UseRecord(int uid, int stationId, String begintime, String endtime, int money) {
        this.uid = uid;
        this.stationId = stationId;
        this.begintime = begintime;
        this.endtime = endtime;
        this.money = money;
    }

    @Override
    public String toString() {
        return "UseRecord{" +
                "uid=" + uid +
                ", stationId=" + stationId +
                ", begintime='" + begintime + '\'' +
                ", endtime='" + endtime + '\'' +
                ", money=" + money +
                '}';
    }

    public void setBegintime(String begintime) {
        this.begintime = begintime;
    }

    public String getEndtime() {
        return endtime;
    }

    public void setEndtime(String endtime) {
        this.endtime = endtime;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
    //main方法
    public static void main(String[] args) throws ParseException, IOException {
        ArrayList<UseRecord> useRecords = new ArrayList<>();
        String s1 = generateTime();
        System.out.println(s1);
        //String s2 = generateEndTime(s1);
        //user： 1-5000 ， station：1-2000
        // 每个用户至少使用一次任意充电桩  充电时间都在2021年
        String bt="";
        String et="";
        int m=0;
        Random random = new Random();
        //5000个用户生成5000条消费数据
        for (int i = 0; i < 5000; i++) {
            int sid = random.nextInt(2000) + 1;//充电桩id 1-2000 随机选择
            bt  = generateTime();
            et=generateEndTime(bt);
            m=generateMoney(bt,et);
            useRecords.add(new UseRecord(i+1,sid,bt,et,m));
        }
        // 随机生成额外的消费数据，一共有10万条
        for (int i = 0; i < 100000-5000; i++) {
            int uid = random.nextInt(5000) + 1;
            int sid = random.nextInt(2000) + 1;
            bt  = generateTime();
            et=generateEndTime(bt);
            m=generateMoney(bt,et);
            useRecords.add(new UseRecord(uid,sid,bt,et,m));
        }
        //数据保存到文件
        saveUseRecordToCSV(useRecords,"data/charging_record_dataset.csv");



    }

    // 生成随机的日期和时间
    public static String generateTime() throws ParseException {
        Random random = new Random();
        int year = 2021;
        int month = new Random().nextInt(12) + 1;  // 随机数0-11
        int day = new Random().nextInt(28) + 1;  // 随机数0-27
        // 生成随机小时（0-23）
        int h = random.nextInt(24);
        // 生成随机分钟（0-59）
        int m = random.nextInt(60);
        // 生成随机秒（0-59）
        int s = random.nextInt(60);
        String dateStr = year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt = sdf.parse(dateStr);
        return sdf.format(dt);
    }

    //生成结束时间  定义规则：充电开始时间和结束时间间隔不能超过8小时, 至少要充电10分钟
    public static String generateEndTime(String beginTime) throws ParseException {
        Random random = new Random();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //将开始时间的字符串，格式化为日期对象
        Date dt = sdf.parse(beginTime);

        long t1 = dt.getTime();  // 得到该日期距 1970-1-1 0:0:0 的毫秒数
        long l = t1 + random.nextInt(28199000) + 600000;   // 8小时转换为毫秒数 随机生成10分钟-8小时直接的毫秒数
        Date endTime = new Date(l); // 再将得到的毫秒数转换为日期对象
        return sdf.format(endTime); // 日期对象转换为字符串返回
    }

    // 计算充电费用  定义规则：每小时收费8元，不足1小时按1小时计算
    public static int generateMoney(String begin, String end) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //将结束的时间 和 开始的时间 转换成为毫秒数相减，除以1000，得到是两个时间之间相差的秒数
        // 除以 3600 ，得到充电小时
        long sec = (sdf.parse(end).getTime() - sdf.parse(begin).getTime()) / 1000;
        double v = sec / 3600.0;   //2.5 小时

        int m = 0;
        int hour = (int) Math.floor(v);  //向下取整
        m = hour * 8;  // 计算整数部分的充电时间所花费的金额
        if (v - hour > 0) {
            m += 8;
        }
        return m;
    }

    //
    //将消费数据保存为 CSV 文件
    public static void saveUseRecordToCSV(List<UseRecord> urs, String fileName) throws IOException {
        FileWriter fw = new FileWriter(fileName);
        // 逐行写入用户数据
        for (UseRecord ur : urs) {
            fw.write(ur.getUid() + "," + ur.getStationId() + "," + ur.getBegintime() + "," + ur.getEndtime() +","+ur.getMoney()+ "\n");
        }
        fw.close();
        System.out.println("消费数据已保存到 " + fileName);
    }
}
